package com.anton46.collectionitempicker;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainAbility extends Ability {


    CollectionPicker mPicker;
    Text mTextView;

    int counter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mPicker = (CollectionPicker) findComponentById(ResourceTable.Id_collection_item_picker);
        mPicker.setItems(generateItems());
        mPicker.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(Item item, int position) {
                if (item.isSelected) {
                    counter++;
                } else {
                    counter--;
                }
                mTextView.setText(counter + " Items Selected");
            }
        });

        mTextView = (Text) findComponentById(ResourceTable.Id_text);
    }

    private List<Item> generateItems() {
        String ids[] = {"a", "b", "c", "d", "e", "f", "j", "h", "i", "j", "k", "l"};
        String texts[] = {"Friendly Staff", "Cozy place", "Pizza", "Burger", "Ice Cream"
                , "Books", "Goods for working", "Romantic Places", "Japanese food", "Apple", "Good","Next"};

        List<Item> items = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            int random = new Random().nextInt(12);
            items.add(new Item(ids[random], texts[random]));
        }
        return items;
    }
}